<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ page import="java.util.List" %>
<%@ page import="javax.jdo.PersistenceManager" %>
<%@ page import="com.google.appengine.api.users.User" %>
<%@ page import="com.google.appengine.api.users.UserService" %>
<%@ page import="com.google.appengine.api.users.UserServiceFactory" %>
<%@ page import="com.kinglian.apptestg.persitence.PMF" %>
<%@ page import="com.kinglian.apptestg.Content" %>

<html>

  <head>
    <link type="text/css" rel="stylesheet" href="/stylesheets/main.css" />
  </head>
  
  <body>
  <% 
  		UserService userService = UserServiceFactory.getUserService();
  		User user = userService.getCurrentUser();
  		  		
  		if ( user != null ) {
  %>
  <p>Hi,Welcome to King's world <%= user.getNickname()%>!</p>
  <p>You can <a href="<%= userService.createLogoutURL(request.getRequestURI()) %>">sign out</a>.)</p>
  
<%
    } else {
%>
<p>Hello!
<a href="<%= userService.createLoginURL(request.getRequestURI()) %>">Sign in!</a>
<% 
	} 
%>

<%
	PersistenceManager pm = PMF.get().getPersistenceManager();
	String query = "select from " + Content.class.getName() + " order by date desc range 0,5";
	List<Content>  contents = (List<Content>)pm.newQuery(query).execute();
	
	if( contents.isEmpty()) {
%>
<p>No messenges so far!</p>
<% 
	}
	else
	{
		for ( Content con : contents)
		{
			if (con.getAuthor()==null)
			{
				out.println("<p>" + " An anonymous person wrote: " + con.getContent() + " at " + con.getDate() + "</p>");
			}
			else
			{
				out.println("<p>" + "  Visitor: " + con.getAuthor() + " said: " + con.getContent() + " at " + con.getDate() + "</p>");
			}
		}
	}
	
	pm.close();
%>

 <form action="/sign" method="post">
    <div><textarea name="content" rows="3" cols="60"></textarea></div>
    <div><input type="submit" value="Post Greeting" /></div>
  </form>

  </body>
</html>